package o2o.task.sys;

import com.jinyou.utils.common.JYMathDoubleUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.utils.common.HttpClientUtil;
import notice.buzz.NoticeBusiness;
import o2o.cache.sys.SysCustomerCache;
import o2o.dao.PO.sys.SysCustomerInfo;
import o2o.utils.LinuxUtils;
import o2o.web.actions.system.token.MemTokenUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class SystemTask {

    private static final String BASE_ACCEPT_REPORT_URL = "https://jycloud.jinyouapp.com/api/records/base/server/acceptReport";

    //秒  分  时  日  月  星期几  年
    @Scheduled(cron = "0 0 0/3 * * ? ")
    public void shopAutoPop() {
        MemTokenUtils.saveAllToken();
    }


    // 检查磁盘情况
    //秒  分  时  日  月  星期几  年
    @Scheduled(cron = "0 0 * * * ? ")
    public void osSpace() {
        LinuxUtils.Desk desk = LinuxUtils.getDeskUsage();
        if (null == desk) {
            return;
        }
        String rateStr = desk.getUse_rate();
        if (ValidateUtil.isNull(rateStr)) {
            return;
        }
        String total = desk.getTotal();
        if (ValidateUtil.isNull(total)) {
            return;
        }
        String used = desk.getUsed();
        if (ValidateUtil.isNull(used)) {
            return;
        }
        total = total.replace("G", "");
        used = used.replace("G", "");
        double totalInt = Double.parseDouble(total);
        double usedInt = Double.parseDouble(used);
        if (Integer.parseInt(rateStr) > 95 || JYMathDoubleUtils.sub(totalInt, usedInt) < 8) {
            String sysKey = SysCustomerCache.getInstance().getDefaultKey();
            String name = "";
            if (ValidateUtil.isNotNull(sysKey)) {
                SysCustomerInfo sysCustomerInfo = SysCustomerCache.getInstance().getInfo(sysKey);
                if (null != sysCustomerInfo) {
                    name = sysCustomerInfo.getName();
                }
            }
            HashMap<String, String> map = new HashMap<>();
            map.put("to", "jinyoubug@163.com");
            map.put("subject", "磁盘空间不足：" + name);
            map.put("content", name + "-->" + desk.toString());
            // 发送邮件
            HttpClientUtil.sendPost("http://o2otools.waimai101.com/jytools/mail/send", map);
        }
    }


    // 定时删除mongodb的系统消息
    //秒  分  时  日  月  星期几  年
    @Scheduled(cron = "0 0 6 * * ? ")
    public void mongoOldNotice() {
        NoticeBusiness.delNotice(null);

        //定时获取并上报服务器信息
        o2oReportedBase();
    }

    //定时获取并上报服务器信息
    public void o2oReportedBase() {
        String sysAppKey = SysCustomerCache.getInstance().getDefaultKey();
        SysCustomerInfo customerInfo = SysCustomerCache.getInstance().getInfo(sysAppKey);
        if (null == customerInfo) {
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("note", customerInfo.getName());
        // 发送上报信息
        String s = HttpClientUtil.sendPost(BASE_ACCEPT_REPORT_URL, map);
        System.out.println(s);
    }
}
